package com.api.workflow.web;


import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
* 设备变更接口
* */
public class YNPMI002Action extends BaseBean implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("设备变更接口=======mainTableInfoMap:" + com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));

        //明细表数据
        List<Map<String, Object>> detailTableMap = getMaps(requestInfo);
        baseBean.writeLog("设备变更接口=======detailTableMap:"+ com.alibaba.fastjson.JSONObject.toJSONString(detailTableMap));

        RecordSet recordSet = new RecordSet();
        try {

            JSONObject result = new JSONObject();
            JSONObject head = new JSONObject();
            JSONArray item = new JSONArray();


            head.put("ZSQNUM",mainTableInfoMap.get("oadh"));
            head.put("ZSQTYPE",mainTableInfoMap.get("ywlx"));
            head.put("ZSQWERK",mainTableInfoMap.get("sqgc"));
            head.put("ZSQBUKRS",mainTableInfoMap.get("gsdm"));
            head.put("ZSQTXT",mainTableInfoMap.get("sqms"));
            head.put("ZSQLTXT",mainTableInfoMap.get("sqsm"));
            head.put("ZOVALUE",mainTableInfoMap.get("yzhj"));
            head.put("ZNVALUE",mainTableInfoMap.get("jzhj"));
//            head.put("ZDCWERK",mainTableInfoMap.get("dcgc"));
//            head.put("ZDCBUKRS",mainTableInfoMap.get("dcgs"));
            head.put("ZDCUSER",mainTableInfoMap.get("dclxr"));
            head.put("ZOANUM",requestid);
            head.put("ZFDATE",mainTableInfoMap.get("wcrq"));
            head.put("ZCDATE",mainTableInfoMap.get("sqrq"));
            String sqr = mainTableInfoMap.get("sqr").toString();
            String sql1= "select * from hrmresource where id = '" + sqr + "'";
            recordSet.execute(sql1);//申请者工号
            if (recordSet.next()) {
                head.put("ZPMYGH",recordSet.getString("workcode"));
            }

            int count =1;
            for (int i = 0; i < detailTableMap.size(); i++) {
                JSONObject itemObject = new JSONObject();
                Map<String, Object> map = detailTableMap.get(i);
                itemObject.put("ZSQNUM",mainTableInfoMap.get("oadh"));
                itemObject.put("ZLINENUM",count);
                count++;
                itemObject.put("ZEQUNR",map.get("sbbm"));
//                itemObject.put("ZXHEQUI",map.get("sbbm"));
                itemObject.put("ZEQKTX",map.get("sbms"));
                itemObject.put("TYPBZ",map.get("sbxh"));
                itemObject.put("ZNANLNR",map.get("zcbh"));
                itemObject.put("ZANLNR",map.get("zcbh"));
                RecordSet recordSet1 = new RecordSet();
                recordSet1.executeUpdate("update uf_zcxxbd set zczt=2 where zckph=?",map.get("zcbh"));
                itemObject.put("ZXKOSTL",map.get("cbzx"));
                itemObject.put("ZASSETJZ",map.get("jz"));
//                itemObject.put("ZXTPLNR",map.get("fchgnwz"));
                itemObject.put("ZYTPLNR",map.get("fchgnwz"));
//                itemObject.put("ZXTPLNR_TX",map.get("fchgnwzms"));
                itemObject.put("ZYTPLNR_TX",map.get("fchgnwzms"));
                itemObject.put("XZRQ",map.get("fcksrq"));
                itemObject.put("ZXZKSDT",map.get("fcksrq"));
                item.add(itemObject);

            }
            result.put("HEAD",head);
            result.put("ITEM",item);

            baseBean.writeLog("设备变更接口=======result:"+result.toString());
            String post = YNHttpUtils.PMI002(result.toString(), YNHttpUtils.token());
            baseBean.writeLog("设备变更接口=======post:"+post);

            //更新主表qqcs和fhjg
            String updateMainTableName = "update " + tableName + " set  qqcsx = ? , fhjgx = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, result.toString(), post, requestid);

            JSONObject jsonObject1 = JSONObject.parseObject(post);
            String status = jsonObject1.getString("STATUS");
            String message = jsonObject1.getString("MESSAGE");
//            JSONArray aReturn = jsonObject1.getJSONArray("RETURN");
//            for (int i = 0; i < aReturn.length(); i++) {
//                JSONObject jsonObject2 = aReturn.getJSONObject(i);
//                String xh = jsonObject2.getString("ZLINENUM");
//                message = message+":明细行"+xh+jsonObject2.getString("MESSAGE");
//
//            }
            if (!"S".equals(status)) {
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid()+"OA-PMI002设备变更接口");
                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:"+message);
                return Action.FAILURE_AND_CONTINUE;
            }



        }
        catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid()+"OA-PMI002设备变更接口");
            requestInfo.getRequestManager().setMessagecontent("接口异常"+e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }



        return Action.SUCCESS;
    }
    /**
     * 提取明细表全部的数据
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }
}


